package com.group.itms.routes.activities;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.group.itms.routes.adapters.MultiViewTypeAdapter;
import com.group.itms.routes.data.BObject;
import com.group.itms.routes.data.ObjectType;
import com.group.itms.routes.data.Topic;
import com.group.itms.routes.utils.Header;
import com.group.itms.routes.utils.Item;

public class BObjectsActivity extends BaseActivity {

	private static final String TAG = BObjectsActivity.class.getSimpleName();

	public static final String TOPIC_ID = "topic_id";

	private Topic mAggregator;
	private MultiViewTypeAdapter mObjectsAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getIntent().getExtras();
		if (args == null) {
			throw new RuntimeException("activity " + getClass()
					+ " is missing arguments");
		}
		int topicId = args.getInt(TOPIC_ID, -1);
		if (topicId == -1) {
			throw new RuntimeException("Can't find element with key: "
					+ TOPIC_ID + " in args");
		}
		mAggregator = getTopic(topicId);
		if (mAggregator == null) {
			throw new RuntimeException("Can't find topic with id: " + topicId);
		}
		
		mObjectsAdapter = new MultiViewTypeAdapter(this);
		List<BObject> objects = queryForObjects(mAggregator);
		List<ObjectType> types = queryForAllTypes();
		for (ObjectType type : types) {
			List<BObject> fObjects = filterBy(objects, type);
			if (fObjects.size() > 0) {
				mObjectsAdapter.add(new Header(type.getName()));
				mObjectsAdapter.addAll(toItems(fObjects));
			}
		}

		ListView objectsListView = new ListView(this);
		objectsListView.setAdapter(mObjectsAdapter);
		objectsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Object itemData = mObjectsAdapter.getItemData(position);
				if (itemData instanceof BObject) {
					BObject object = (BObject) itemData;
					ObjectType type = object.getType();
					int objectId = object.getId();
					Context ctx = BObjectsActivity.this;
					Intent intent = null;
					if (type.equals(ObjectType.LOCATION)) {
						intent = LocationDetailActivity.getIntent(ctx, objectId);
					} else if (type.equals(ObjectType.ROUTE)) {
						intent = RouteActivity.getIntent(ctx, objectId);
					} else if (type.equals(ObjectType.INF_POINT)) {
						intent = InformationPointActivity.getIntent(ctx, objectId);
					} else if (type.equals(ObjectType.TASK)) {
						BObject taskParent = object.getParent();
						if (taskParent != null) {
							intent = TasksActivity.getIntent(ctx, taskParent.getId());
						}
					}
					if (intent != null) {
						startActivity(intent, true);
					}
				}
				
			}
		});
		setContentView(objectsListView);
	}

	private List<ObjectType> queryForAllTypes() {
		try {
			return getHelper().getTypeDao().queryForAll();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private List<BObject> queryForObjects(Topic topic) {
		List<BObject> fObjects = new ArrayList<BObject>();
		List<BObject> objects = topic.getObjects();
		for (BObject o : objects) {
			refreshObject(o);
			if (o.isActive()) {
				fObjects.add(o);
			}
		}
		return fObjects;
	}

	private List<BObject> filterBy(List<BObject> objects, ObjectType type) {
		List<BObject> filteredObjects = new ArrayList<BObject>();
		for (BObject obj : objects) {
			if (obj.getType().equals(type)) {
				filteredObjects.add(obj);
			}
		}
		return filteredObjects;
	}

	private List<Item> toItems(List<BObject> objects) {
		List<Item> items = new ArrayList<Item>();
		for (BObject obj : objects) {
			items.add(new Item(obj.getName(), obj));
		}
		return items;
	}

	@Override
	protected String getTag() {
		return TAG;
	}
}
